{extend name="layout"}
{block name="style"}{/block}
{block name="left_main"}
  <ul class="layui-nav layui-nav-tree" style="width:100%;">
    <li class="layui-nav-item {if in_array(request()->action(),['index'])}layui-this{/if}"><a href="{:url('index/index')}">主页</a></li>
    <li class="layui-nav-item {if in_array(request()->action(),['menu'])}layui-this{/if}"><a href="{:url('system/menu')}">菜单管理</a></li>
    <li class="layui-nav-item {if in_array(request()->action(),['wechat'])}layui-this{/if}"><a href="{:url('system/wechat')}">公众号管理</a></li>
    <li class="layui-nav-item {if in_array(request()->action(),['users'])}layui-this{/if}"><a href="{:url('system/users')}">用户管理</a></li>
    <li class="layui-nav-item {if in_array(request()->action(),['ad'])}layui-this{/if}"><a href="{:url('system/ad')}">广告图管理</a></li>
    <li class="layui-nav-item {if in_array(request()->action(),['lang'])}layui-this{/if}"><a href="{:url('system/lang')}">多语言管理</a></li>
    <li class="layui-nav-item {if in_array(request()->action(),['databases'])}layui-this{/if}"><a href="{:url('system/databases')}">数据库管理</a></li>
    <li class="layui-nav-item {if in_array(request()->action(),['conf'])}layui-this{/if}"><a href="{:url('system/conf')}">系统配置</a></li>
  </ul>
{/block}
{block name="right_main"}

<div class="layui-tab layui-tab-brief" style="margin:0;box-shadow:none;border:0;">
  <ul class="layui-tab-title">
    <li class="layui-this">数据库备份</li>
  </ul>
  <div class="layui-tab-content" style="padding:20px;background:#fff;">
    <fieldset class="layui-elem-field site-demo-button" style="padding:20px;margin-bottom:20px;">
      <legend>操作</legend>
      <div>
        <button class="layui-btn layui-btn-primary" id="backup">备份数据</button>
        <button class="layui-btn layui-btn-primary" id="batchDelete">批量删除</button>
      </div>
    </fieldset>


    <table class="layui-table" lay-data="{skin: 'line',method:'post', url:'{:url('system/databases',input('get.'))}', page:true, id:'table'}" lay-filter="table">
      <thead>
        <tr>
          <th lay-data="{checkbox:true, fixed: true}"></th>
          <th lay-data="{field:'name', align:'center', width:200}">Name</th>
          <th lay-data="{field:'size', align:'center', width:200}">Size</th>
          <th lay-data="{field:'time', align:'center', width:200}">Time</th>
          <th lay-data="{width:306, align:'center', toolbar: '#barDemo'}">操作</th>
        </tr>
      </thead>
    </table>
    <script type="text/html" id="barDemo">
      <a class="layui-btn layui-btn-warm layui-btn-mini" lay-event="download">下载</a>
      <a class="layui-btn layui-btn-normal layui-btn-mini" lay-event="recovery">恢复</a>
      <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="delete">删除</a>
    </script>
    <script>
      layui.use(['table','jquery'], function(){
        var table = layui.table;
        var $ = layui.jquery;

        $('#backup').on('click',function(){
          layer.open({
            type: 1
            ,title:'数据库备份'
            ,content: '<div style="padding: 50px 100px;">请不要关闭本弹窗！系统会自动关闭，数据正在备份中...<i style="font-size:16px;" class="layui-icon layui-anim layui-anim-rotate layui-anim-loop">&#xe63d;</i></div>'
            ,btn: ''
            ,btnAlign: 'r' //按钮居中
            ,shade: .6 //不显示遮罩
          });
          $.post('{:url('system/backupDatabase')}',{},function(res){
            if(res.code==1){
              layer.closeAll();
              location.href = location;
            }else{
              layer.msg(res.msg,{icon:5,anim:6});
            }
          })
        })

        $('#batchDelete').on('click',function(){
          var checkStatus = table.checkStatus('table'),data = checkStatus.data;
          if(data.length<1){
              layer.msg('未选中要删除的数据！',{icon:5,anim:6});
              return false;
          }
          var name = new Array();
          for(var i=0;i<data.length;i++){
            name.push(data[i].name);
          }
          layer.alert();
          name = name.toString();
          layer.confirm('确定要删除ID：'+name, function(index){
            $.post('{:url('system/deleteDatabase')}',{name:name},function(res){
              layer.close(index);
              if(res.code==1){
                location.href = location;
              }else{
                layer.msg(res.msg);
              }
            })
          });
        })
        table.on('tool(table)', function(obj){
          var data = obj.data;
          if(obj.event === 'delete'){
            layer.confirm('确认真的删除吗？', function(index){
              $.post('{:url('system/deleteDatabase')}',{name:data.name},function(res){
                if(res.code==1){
                  obj.del();
                  layer.close(index);
                }else{
                  layer.msg(res.msg);
                  layer.close(index);
                }
              })
            });
          }else if(obj.event === 'recovery'){
            layer.confirm('确认真的恢复到此数据库版本吗？', function(index){
              $.post('{:url('system/recoveryDatabase')}',{name:data.name},function(res){
                layer.msg(res.msg);
                layer.close(index);
              })
            });
          }else if(obj.event === 'download'){
            location.href = '{:url('system/downloadDatabase')}?name='+data.name;
          }
        });
      });
    </script>


  </div>

</div>
{/block}
{block name="script"}
  <script>
    layui.use(['form', 'layedit', 'laydate'], function(){
      var form = layui.form
      ,layer = layui.layer
      ,laydate = layui.laydate;
      //日期
      laydate.render({
        elem: '#date'
      });
    });
  </script>
{/block}